package com.brillilab.service.logic.method;

import com.brillilab.domain.po.method.MethodTypePower;
import com.brillilab.domain.vo.method.MethodTypePowerList;
import com.brillilab.domain.vo.method.MethodTypePowerVo;
import com.brillilab.service.core.method.IMethodTypePowerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class MethodTypePowerLogic {

    @Resource
    private IMethodTypePowerService methodTypePowerService;

    /**
     *  成员方法权限列表
     * @param labMemberId
     * @return
     *  */
    public MethodTypePowerList getMemberMethodPowerList(Long labMemberId){
        Assert.isTrue( labMemberId != null,"参数缺失！");
        List<MethodTypePowerVo> open=methodTypePowerService.selectMethodTypePowerVo(labMemberId);
        List<MethodTypePowerVo> allList=methodTypePowerService.selectAllFirstMethodType();
        List<MethodTypePowerVo> notOpen=allList.stream().filter(power -> !open.contains(power)).collect(Collectors.toList());
        MethodTypePowerList list=new MethodTypePowerList();
        list.setOpen(open);
        list.setNotOpen(notOpen);

        return list;
    }

    /**
     *修改方法类型
     * @param labMemberId
     * @param typeIds
     * @return
     *  */
    public Boolean editMemberPower(List<Long> typeIds,Long labMemberId){
        Assert.isTrue(labMemberId != null,"参数缺失！");
        boolean rs = methodTypePowerService.deleteByLabMemberId(labMemberId);
        Assert.isTrue(rs,"业务操作失败！");

        if(typeIds!=null){

            List<MethodTypePower> list=new ArrayList<MethodTypePower>();
            for (Long methodTypeId :typeIds){
                MethodTypePower methodTypePower=new MethodTypePower();
                methodTypePower.setLabMemberId(labMemberId);
                methodTypePower.setFirstMethodTypeId(methodTypeId);
                list.add(methodTypePower);
            }
            return methodTypePowerService.addMethodTypePower(list);
        }else{
            return true;
        }

    }
}
